package com.yootk.lombok;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.Synchronized;

import java.util.concurrent.TimeUnit;

@Data
@AllArgsConstructor
public class SaleTicket {
    private int ticket;     // 总票数

    /**
     * 售票操作
     */
    @SneakyThrows
    @Synchronized   // 同步处理
    public void sale() {
        if (this.ticket > 0) {
            TimeUnit.SECONDS.sleep(1);
            System.err.printf("【%s】售票，ticket = %d%n", Thread.currentThread().getName(), this.ticket--);
        } else {
            System.err.printf("【%s】票已卖完%n", Thread.currentThread().getName());
        }
    }
}
